<template>
	<el-dialog v-model="showModal" class="peak-dialog" :close-on-click-modal="false" :title="title" :width="900">
		<el-form ref="paramsFormRef" class="params-form" :inline="true" label-position="right" label-width="90px" :model="paramsForm" :rules="paramsRules">
			<el-row :gutter="16" justify="start">
				<el-col :span="12">
					<el-form-item class="form-item" label="字典名称:" prop="dictName">
						<el-input v-model="paramsForm.dictName" placeholder="请输入字典名称" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="字典类型:" prop="dictType">
						<el-input v-model="paramsForm.dictType" placeholder="请输入字典类型" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="可用状态:" prop="status">
						<el-radio-group v-model="paramsForm.status">
							<el-radio :value="'0'">正常</el-radio>
							<el-radio :value="'1'">停用</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="form-item" label="备注信息:" prop="remark">
						<el-input v-model="paramsForm.remark" rows="3" type="textarea" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleCancel">取 消</el-button>
				<el-button type="primary" @click="handleSure" v-throttle>确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script name="EditDictTypeModal" setup>
import global from '@/utils/global'
const emit = defineEmits(['peak-modal-ok'])
/*
 *@Description: 初始化数据
 *@Author: liuYuFeng
 *@Date: 2023-02-24 16:52:40
 */
const state = reactive({
	showModal: false,
	paramsFormRef: null,
	title: '',
	paramsForm: {
		dictId: '',
		dictName: '',
		dictType: '',
		status: '0',
		remark: '',
	},
	paramsRules: {
		dictName: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
		dictType: [{ required: true, message: '请输入字典类型', trigger: 'blur' }],
	},
})
const { showModal, title, paramsForm, paramsRules, paramsFormRef } = toRefs(state)

/*
 *@Description: 方法method
 *@Author: liuYuFeng
 *@Date: 2023-02-24 15:53:16
 */
const handleSure = () => {
	state.paramsFormRef.validate(valid => {
		if (valid) {
			handleSureOk()
		} else {
			return false
		}
	})
}
const handleSureOk = () => {
	if (state.paramsForm.dictId) {
		//编辑
		global.$fetchPostData(`/meta-system/web/v1/sysDictType/editDictType`, state.paramsForm).then(data => {
			if (data.code == 200) {
				state.showModal = false
				emit('peak-modal-ok', 2)
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
	} else {
		global.$fetchPostData(`/meta-system/web/v1/sysDictType/addDictType`, state.paramsForm).then(data => {
			if (data.code == 200) {
				state.showModal = false
				emit('peak-modal-ok', 1)
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
	}
}
const handleCancel = () => {
	state.showModal = false
}
const handleShowModal = async row => {
	state.showModal = true
	await nextTick()
	state.paramsForm.dictId = ''
	if (row) {
		state.title = '编辑字典类型'
		state.paramsFormRef.resetFields()
		getDataDetail(row.dictId)
	} else {
		state.title = '添加字典类型'
		state.paramsFormRef.resetFields()
	}
}
const getDataDetail = dictId => {
	global.$fetchPostData(`/meta-system/web/v1/sysDictType/details`, { idLong: dictId }).then(data => {
		if (data.code == 200) {
			state.paramsForm = data.data || {}
		} else {
			global.$baseMessage(data.msg, 'error')
		}
	})
}
defineExpose({
	handleShowModal,
})
</script>
<style lang="scss" scoped></style>
